<template>
  <van-dialog
    class="com-dialog"
    v-model="show"
    :overlay="true"
    :width="width"
    :show-confirm-button="false"
    :close-on-click-overlay="true"
    :beforeClose="beforeClose"
  >
    <div class="dialog-body1" v-if="dialog1">
      <div class="header font-size15">
        {{ title }}
      </div>
      <div class="content font-size13">
        {{ content }}
      </div>
      <div class="footer font-size14">
        <div class="btn-left" @click="handleClose">取消</div>
        <div class="btn-right" @click="handleConfirm">确定</div>
      </div>
    </div>
    <div class="dialog-body2" v-if="dialog2">
      <div class="header-img">
        <img :src="imgsrc" alt="" />
      </div>
      <div class="main-title font-size15">
        {{ title }}
      </div>
      <div class="content-box">
        <div class="content font-size13">
          {{ content }}
        </div>
      </div>
      <div class="footer" v-if="btnShow">
        <div class="btn-center font-size13" @click="handleDefine">
          {{ btnTitle }}
        </div>
      </div>
    </div>
  </van-dialog>
</template>

<script>
export default {
  name: "ComDialog",
  props: {
    close: Function,
    show: Boolean,
    //说明下dialog1为表单提交时的确认弹窗，不带图片；dialog2为请求后端时校验失败弹窗或者返回结果弹窗，带图片；其中dialog2中某几个场景中是不我知道了按钮的。
    dialog1: Boolean,
    dialog2: Boolean,
    btnShow: Boolean,
    title: {
      type: String,
      default: (_) => "消息框",
    },
    width: {
      type: [String, Number],
      default: (_) => "30vw",
    },
    imgsrc: {
      type: String,
      default: (_) => "",
    },
    content: {
      type: String,
      default: (_) => "",
    },
    btnTitle: {
      type: String,
      default: (_) => "",
    },
  },
  methods: {
    handleClose() {
      console.log("[点击事件1次弹窗] - 取消1");
      this.$emit("dialog-close");
    },
    handleConfirm() {
      console.log("[点击事件1次弹窗] - 确认1");
      this.$emit("dialog-confirm");
    },
    handleDefine() {
      console.log("[点击事件2次弹窗] - 确认1");
      this.$emit("handle-define");
    },
    beforeClose(action, done) {
      // 点击事件 - 目前把该组件原生的取消和确认按钮去掉了，所以没有下面的confirm和cancel
      if (action === "confirm") {
        this.$emit("dialog-confirm");
        done(); // 取消后关闭提示框
      } else if (action === "cancel") {
        this.$emit("dialog-close");
        done(); //确定后关闭提示框
      } else {
        //overlay 点击遮罩层关闭弹窗
        this.$emit("dialog-close");
      }
    },
  },
};
</script>

<style scoped lang="less">
.com-dialog {
  // height: 118px;
  background: #ffffff;
  border-radius: 10px;
}
.dialog-body1 {
  .header {
    text-align: center;
    padding-top: 22px;
    font-weight: bold;
  }
  .content {
    margin: 15px 20px;
    font-weight: 500;
    line-height: 15px;
  }
  .footer {
    font-weight: 500;
    color: #118a84;
    line-height: 41px;
    display: flex;
    height: 40px;
    justify-content: center;
    .btn-right,
    .btn-left {
      width: 100%;
      text-align: center;
      border-top: 1px solid #eeeeee;
    }
    .btn-left {
      border-right: 1px solid #eeeeee;
    }
  }
}
.dialog-body2 {
  .header-img {
    padding: 24px;
    text-align: center;
    > img {
      width: 141px;
      height: 70px;
    }
  }
  .main-title {
    text-align: center;
    margin-top: 3px;
    font-weight: bold;
  }
  .content-box {
    display: flex;
    justify-content: center;
    .content {
      margin: 15px 24px 30px 24px;
      font-weight: 500;
      line-height: 15px;
    }
  }
  .footer {
    margin-top: -15px;
    text-align: center;
    padding: 0 19px 19px 19px;
    .btn-center {
      height: 28px;
      background: #118a84;
      border-radius: 4px;
      font-weight: 500;
      color: #ffffff;
      line-height: 28px;
    }
  }
}
</style>
